package blog.gui;

import javax.microedition.lcdui.*;

import blog.*;
import blog.controller.*;
import blog.db.*;

public class ShortCutListGui extends List implements IBaseGui, CommandListener {
	
	private static Command cmdBack = new Command( ConstString.CMD_BACK, Command.BACK, 0);
	private static Command cmdAdd = new Command( ConstString.CMD_NEW_ENTRY, Command.SCREEN, 0);
	private static Command cmdEdit = new Command( ConstString.CMD_EDIT, Command.SCREEN, 1);
	private static Command cmdDelete = new Command( ConstString.CMD_REMOVE, Command.SCREEN, 2);
	private static Command cmdSelect = new Command( ConstString.CMD_SELECT, Command.SCREEN, 3 );
	private static Command cmdSave_add = new Command( ConstString.CMD_SAVE, Command.SCREEN, 0 );
	private static Command cmdSave_edit = new Command( ConstString.CMD_SAVE, Command.SCREEN, 0 );
	
	private ShortCut[] data;
	private int insertPos;
	private Image img;

	private ShortCutListGui() {
		super(ConstString.GUI_TITLE_SHORT_CUT_LIST, List.IMPLICIT);
		
		this.setCommandListener(this);
		addCommand(cmdBack);
		addCommand(cmdAdd);
		try {img = Image.createImage("/shortcut_new.png");} catch(Exception e){}
	}
	
	private static ShortCutListGui _instance;
	public static synchronized ShortCutListGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new ShortCutListGui();
		}
		return _instance;
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_SHORT_CUT_LIST;
	}

	public void notifyChanged(Object param) {

	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdBack )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==List.SELECT_COMMAND || cmd==cmdSelect )
		{
			AdvanceBodyEdit.getInstance().insertSomething( data[this.getSelectedIndex()].content, insertPos );
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdAdd )
		{
			ShortCut sc = new ShortCut();
			sc.content = "";
			ShortCutGui scg = new ShortCutGui( sc );
			scg.addCommand(cmdSave_add);
			scg.addCommand(cmdBack);
			SysRuntime.guiManager.show(scg);
		}
		else if ( cmd==cmdEdit )
		{
			ShortCut sc = this.data[this.getSelectedIndex()];
			ShortCutGui scg = new ShortCutGui( sc );
			scg.addCommand(cmdBack);
			scg.addCommand(cmdSave_edit);
			SysRuntime.guiManager.show(scg);
		}
		else if ( cmd==cmdDelete )
		{
			ShortCutDB.delete(data[this.getSelectedIndex()]);
			this.reloadData();
		}
	}
	
	public void reloadData()
	{
		data = ShortCutDB.getAllIndex();
		
		this.deleteAll();
		this.removeCommand(cmdDelete);
		removeCommand(cmdEdit);
		removeCommand(cmdSelect);
		
		if ( data.length>0 )
		{
			addCommand(cmdDelete);
			addCommand(cmdEdit);
			addCommand(cmdSelect);
			
			for( int i=0; i<data.length; i++ )
			{
				this.append(data[i].content, img);
			}
		}
		else
		{
			append( ConstString.GUI_SHORTCUT_LIST_NO_DATA, null );
		}
	}

	public void setInsertPos(int insertPos) {
		this.insertPos = insertPos;
	}
	
	static class ShortCutGui extends TextBox implements CommandListener
	{
		ShortCut model;
		
		public ShortCutGui(ShortCut model) {
			super(ConstString.GUI_TITLE_SHORT_CUT_LIST, model.content, 200, TextField.ANY);
			this.model = model;
			this.setCommandListener(this);
		}

		public void commandAction(Command cmd, Displayable arg1) {
			if ( cmd==ShortCutListGui.cmdBack )
			{
				SysRuntime.guiManager.back();
			}
			else if ( cmd==ShortCutListGui.cmdSave_add )
			{
				model.content = this.getString();
				model.visitCount = 0;
				ShortCutDB.add(model);
				ShortCutListGui.getInstance().reloadData();
				SysRuntime.guiManager.back();
			}
			else if ( cmd==ShortCutListGui.cmdSave_edit )
			{
				model.content = this.getString();
				ShortCutDB.udpate(model);
				ShortCutListGui.getInstance().reloadData();
				SysRuntime.guiManager.back();
			}
		}
		
	}

}